package com.sky.controller.user;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.sky.result.Result;
import com.sky.service.user.UserOrderService;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2024/10/17 15:55
 * @Version 1.0
 */
@RestController
@Slf4j
public class WxPayController {

    @Autowired
    private Config config;
   @Autowired
    private NotificationConfig notificationConfig;

   @Autowired
   private UserOrderService userOrderService;

   //http://localhost:8080/wx/notify

    @GetMapping("/paySuc")
    public String mockPaySuccess(String num){
        log.info("模拟用户支付成功，通知前端");
        userOrderService.paySuccess(num);
        return "OK";
    }

   @PostMapping("/wx/notify")
    public String wxNotify(@RequestBody String body, HttpServletRequest request){
        log.info("收到微信回调消息");
        String wechatSignature = request.getHeader("Wechatpay-Signature");
        String wechatPaySerial = request.getHeader("Wechatpay-Serial");
        String wechatpayNonce = request.getHeader("Wechatpay-Nonce");
        String wechatTimestamp = request.getHeader("Wechatpay-Timestamp");

        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber(wechatPaySerial)
                .nonce(wechatpayNonce)
                .signature(wechatSignature)
                .timestamp(wechatTimestamp)
                .body(body)
                .build();

        NotificationParser notificationParser = new NotificationParser(notificationConfig);

        Transaction transaction = notificationParser.parse(requestParam,Transaction.class);
        String outTradeNo = transaction.getOutTradeNo();
        log.info("订单{}支付成功，进行后续处理",outTradeNo);
        //todo 后续订单相关操作，改支付状态，发货，根据业务处理
        userOrderService.paySuccess(outTradeNo);
        // 处理成功，返回 200 OK 状态码
        return ResponseEntity.status(HttpStatus.OK).toString();
    }

    @GetMapping("/wxpay")
    public Result wxPay(){
        NativePayService payService = new NativePayService.Builder().config(config).build();
        //构造参数
        PrepayRequest request = new PrepayRequest();
        request.setOutTradeNo("2322222322222223547");
        request.setDescription("iphone 16pro 512G");
        request.setNotifyUrl("https://5cbfe97d.r12.cpolar.top/wx/notify");
        request.setAppid("wx0ca99a203b1e9943");
        request.setMchid("1561414331");
        Amount amount = new Amount();
        amount.setTotal(1);
        amount.setCurrency("CNY");
        request.setAmount(amount);

        PrepayResponse response = payService.prepay(request);
        String codeUrl = response.getCodeUrl();
        QrCodeUtil.generate(codeUrl,800,800,new File("D://pay.jpg"));

        return Result.success();
    }

    @GetMapping("/query")
    public Result query(String num){
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchid("1561414331");
        request.setOutTradeNo(num);

        NativePayService payService = new NativePayService.Builder().config(config).build();
        Transaction transaction = payService.queryOrderByOutTradeNo(request);

        return Result.success(transaction);
    }

    @GetMapping("/refound")
    public Result refund(){
        RefundService refundService = new RefundService.Builder().config(config).build();

        CreateRequest createRequest = new CreateRequest();
        //构造退款参数
        createRequest.setNotifyUrl("https://5cbfe97d.r12.cpolar.top/wx/notify");
        createRequest.setOutTradeNo("2322222322222223547");
        createRequest.setOutRefundNo("122322222322222223547");
        createRequest.setReason("就是要退");
        AmountReq amountReq = new AmountReq();
        amountReq.setRefund(1L);
        amountReq.setTotal(1L);
        amountReq.setCurrency("CNY");
        createRequest.setAmount(amountReq);
        Refund refund = refundService.create(createRequest);
        return Result.success(refund);
    }
}